﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OSGeo.MapGuide;
using System.IO;
using System.Configuration;

namespace NegevGas.Core
{
    public static class HtzFileManager
    {
        // Fields
        private static readonly string SpatialDataFolder = ConfigurationManager.AppSettings["SpatialDataFolder"];
        private static readonly string FilesRepository = Path.Combine(SpatialDataFolder, ConfigurationManager.AppSettings["FilesRepository"]);
        private static readonly string FilesRepositoryupload =  ConfigurationManager.AppSettings["FilesRepositoryupload"];
       

        // Methods
        public static void AddFile(Stream input, string projectName, string mapName, string featureSource, long uid, string fileName, string description)
        {
            HtzFile file = HtzFile.AddFile(projectName, mapName, featureSource, uid, fileName, description);
            if (file != null)
            {
                string destinationPath = Path.GetDirectoryName(file.PathToFile);
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                FileStream output = new FileStream(destinationPath+fileName, FileMode.OpenOrCreate, FileAccess.Write);
                using (BinaryWriter writer = new BinaryWriter(output))
                {
                    using (BinaryReader reader = new BinaryReader(input))
                    {
                        byte[] buffer = new byte[input.Length];
                        reader.Read(buffer, 0, buffer.Length);
                        writer.Write(buffer);
                        writer.Flush();
                    }
                }
            }
        }

        public static void DeleteFile(string projectName, string featureSource, long featureId, long fileId)
        {
             string FilesRepositoryupload =  ConfigurationManager.AppSettings["FilesRepositoryupload"];
            string fileName = HtzFile.DeleteFile(projectName, fileId);
            if (!string.IsNullOrEmpty(fileName))
            {
                DBUploadFiles db = new DBUploadFiles(projectName);
                string filePath = FilesRepositoryupload +string.Format(@"{0}\{1}\{2}\{3}", new object[] { projectName, db.GetFeatureSourceId(featureSource, false), featureId, fileName });
                string folderPath = Path.GetDirectoryName(filePath);
                File.Delete(filePath);
                if (Directory.GetFiles(folderPath).Length == 0)
                {
                    Directory.Delete(folderPath);
                }
            }
        }

        public static int GetUidFromSelection(HtzMgHelper helper, string idField)
        {
            return GetUidFromSelection(helper, helper.Selection, idField);
        }

        public static int GetUidFromSelection(HtzMgHelper helper, MgSelection selection, string idField)
        {
            MgLayer layer = selection.GetLayers()[0] as MgLayer;
            if (layer == null)
            {
                return 0;
            }
            MgFeatureQueryOptions opt = new MgFeatureQueryOptions();
            opt.SetFilter(selection.GenerateFilter(layer, layer.FeatureClassName));
            MgFeatureReader reader = helper.FeatureService.SelectFeatures(new MgResourceIdentifier(layer.FeatureSourceId), layer.FeatureClassName, opt);
            reader.ReadNext();
            int result = (int)helper.GetFeaturePropertyValue(reader, idField);
            reader.Close();
            reader.Dispose();
            return result;
        }

        public static void UpdateFile(string projectName, long id, string description)
        {
            HtzFile.UpdateFile(projectName, id, description);
        }
    }

 

}